import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPRow;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;


/**
 * 惠州市水产良种推进工程项目验收表
 * @author zhangmz
 */
public class AquaticProductAcceptancePDF {
	
	/**
	 * 第一页
	 * @param doc 	doc 内容
	 * @param bank_par  换行
	 * @param titleFont 标题字体设置
	 * @param font		内容字体设置
	 * @param date		日期
	 * @param sdf		日期格式
	 * @throws DocumentException
	 * @throws IOException
	 */
	@SuppressWarnings({  "static-access", "unchecked" })
	private static void page1(Document doc,Paragraph bank_par,Font titleFont,Font font,Date date,SimpleDateFormat sdf) throws DocumentException, IOException{
		doc.add(bank_par);
		
		Paragraph titleP = new Paragraph(" 惠州市水产良种推进工程\n项目验收表\n\n",
				titleFont);
		titleP.setAlignment(titleP.ALIGN_CENTER);
		doc.add(titleP);
		
		doc.add(bank_par);
		
		PdfPTable table = new PdfPTable(4);
		table.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.setWidthPercentage(80);
		int width[] = {30,40,0,0};//设置每列宽度比例   
		table.setWidths(width);  
		
		PdfPCell cell = new PdfPCell(new Paragraph("项目名称：", font));
		cell.setBorder(0);
		PdfPCell cell2 = new PdfPCell(new Paragraph("测试项目名称啊啊啊啊啊啊啊", font));
		cell2.setColspan(3);
		cell2.setBorder(0);
		cell2.enableBorderSide(2);
		
		table.addCell(cell);
		table.addCell(cell2);
		
		cell = new PdfPCell(new Paragraph("项目承担单位：", font));
		cell.setBorder(0);
		cell2 = new PdfPCell(new Paragraph("甲方单位XXXXXXXXXXXX", font));
		cell2.setColspan(3);
		cell2.setBorder(0);
		cell2.enableBorderSide(2);
		
		table.addCell(cell);
		table.addCell(cell2);
		
		cell = new PdfPCell(new Paragraph("项目主管部门：", font));
		cell.setBorder(0);
		cell2 = new PdfPCell(new Paragraph("主管单位XXXXXXXXXXX", font));
		cell2.setColspan(3);
		cell2.setBorder(0);
		cell2.enableBorderSide(2);
		
		table.addCell(cell);
		table.addCell(cell2);
		
		cell = new PdfPCell(new Paragraph("验收组织单位（公章）：", font));
		cell.setBorder(0);
		cell2 = new PdfPCell(new Paragraph("验收单位XXXXXXXXXXX", font));
		cell2.setColspan(3);
		cell2.setBorder(0);
		cell2.enableBorderSide(2);
		
		table.addCell(cell);
		table.addCell(cell2);
		for (PdfPRow row : (ArrayList<PdfPRow>) table.getRows()) {
			for (PdfPCell cells : row.getCells()) {
				if (cells != null) {
					cells.setPadding(8.0f);
				}
			}
		}
		doc.add(table);
		//空白 四行
		doc.add(bank_par);
		doc.add(bank_par);
		
		BaseFont bf_booder = BaseFont.createFont("STSong-Light",
				"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		Font bf_booderFont = new Font(bf_booder, 18, Font.NORMAL);
		Paragraph FirstPage_booder_Time = new Paragraph("填报时间：          年      月      日\n\n\n", bf_booderFont);
		FirstPage_booder_Time.setAlignment(FirstPage_booder_Time.ALIGN_CENTER);
		Paragraph FirstPage_booder = new Paragraph("惠州市海洋与渔业局 \n" +
				sdf.format(date)+" 年 制\n\n", bf_booderFont);
		FirstPage_booder.setAlignment(FirstPage_booder.ALIGN_CENTER);
		doc.add(FirstPage_booder_Time);
		doc.add(FirstPage_booder);
	}
	
	private static void page2(Document doc,Font Sec_titleFont,Font font) throws DocumentException{
		doc.newPage();
		PdfPTable table = new PdfPTable(4);
		table.setWidthPercentage(90);
		table.getDefaultCell().setMinimumHeight(30);
		table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.setSplitLate(false);		
		int widths[] = {19,16,9,12};//设置每列宽度比例   
        table.setWidths(widths);  
		
        PdfPCell cell = new PdfPCell(new Paragraph("一、项目基本情况表\n\n",Sec_titleFont));
        cell.setColspan(4);
        cell.setBorder(0);
        cell.setPaddingTop(50);
        table.addCell(cell);
        
		table.addCell(new Paragraph("项目名称：",font));
		cell = new PdfPCell(new Paragraph(" ",font));
		cell.setColspan(3);
		table.addCell(cell);
		
		table.addCell(new Paragraph("承担单位：",font));
		cell = new PdfPCell(new Paragraph(" ",font));
		cell.setColspan(3);
		table.addCell(cell);
		
		table.addCell(new Paragraph("项目主管部门：",font));
		cell = new PdfPCell(new Paragraph(" ",font));
		cell.setColspan(3);
		table.addCell(cell);
		
		table.addCell(new Paragraph("承担单位性质：",font));
		cell = new PdfPCell(new Paragraph("行政",font));
		cell.setColspan(3);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.addCell(cell);
		
		table.addCell(new Paragraph("承担单位法定代表人：",font));
		table.addCell(new Paragraph(" ",font));
		table.addCell(new Paragraph("电话：",font));
		table.addCell(new Paragraph(" ",font));
		
		table.addCell(new Paragraph("承担单位通信地址：",font));
		table.addCell(new Paragraph(" ",font));
		table.addCell(new Paragraph("邮编：",font));
		table.addCell(new Paragraph(" ",font));
		
		table.addCell(new Paragraph("项目下达时间、文号：",font));
		cell = new PdfPCell(new Paragraph(" ",font));
		cell.setColspan(3);
		table.addCell(cell);
		
		table.addCell(new Paragraph("项目下达资金（万元）：",font));
		table.addCell(new Paragraph(" ",font));
		table.addCell(new Paragraph("建设年限：",font));
		table.addCell(new Paragraph(" ",font));
		
		table.addCell(new Paragraph("下达的建设内容及规模：",font));
		cell = new PdfPCell(new Paragraph(" ",font));
		cell.setColspan(3);
		cell.setMinimumHeight(200);
		table.addCell(cell);
		
		cell = new PdfPCell(new Paragraph("项目验收组织单位：",font));
		cell.setBorderWidthBottom(0);
		cell.setBorderWidthRight(0);
		cell.setMinimumHeight(40);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		PdfPCell cell2 = new PdfPCell(new Paragraph(" ",font));
		cell2.setColspan(3);
		cell2.setBorderWidthLeft(0);
		cell2.setBorderWidthBottom(0);
		table.addCell(cell);
		table.addCell(cell2);
		
		cell = new PdfPCell(new Paragraph("联系人：",font));
		cell.setBorderWidthTop(0);
		cell.setBorderWidthRight(0);
		cell.setMinimumHeight(40);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell2 = new PdfPCell(new Paragraph(" ",font));
		cell2.setBorderWidthTop(0);
		cell2.setBorderWidthLeft(0);
		cell2.setBorderWidthRight(0);
		cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.addCell(cell);
		table.addCell(cell2);
		
		cell = new PdfPCell(new Paragraph("电话：",font));
		cell.setBorderWidthTop(0);
		cell.setBorderWidthLeft(0);
		cell.setBorderWidthRight(0);
		cell.setMinimumHeight(40);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell2 = new PdfPCell(new Paragraph(" ",font));
		cell2.setBorderWidthTop(0);
		cell2.setBorderWidthLeft(0);
		cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.addCell(cell);
		table.addCell(cell2);
		
		cell = new PdfPCell(new Paragraph("项目验收组组长：",font));
		cell.setBorderWidthTop(0);
		cell.setBorderWidthBottom(0);
		cell.setBorderWidthRight(0);
		cell.setMinimumHeight(40);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell2 = new PdfPCell(new Paragraph(" ",font));
		cell2.setBorderWidthTop(0);
		cell2.setBorderWidthBottom(0);
		cell2.setBorderWidthLeft(0);
		cell2.setBorderWidthRight(0);
		cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.addCell(cell);
		table.addCell(cell2);
		
		cell = new PdfPCell(new Paragraph("单位：",font));
		cell.setBorderWidthTop(0);
		cell.setBorderWidthBottom(0);
		cell.setBorderWidthLeft(0);
		cell.setBorderWidthRight(0);
		cell.setMinimumHeight(40);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell2 = new PdfPCell(new Paragraph(" ",font));
		cell2.setBorderWidthTop(0);
		cell2.setBorderWidthBottom(0);
		cell2.setBorderWidthLeft(0);
		cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.addCell(cell);
		table.addCell(cell2);
		
		cell = new PdfPCell(new Paragraph("职务：",font));
		cell.setBorderWidthTop(0);
		cell.setBorderWidthRight(0);
		cell.setMinimumHeight(40);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell2 = new PdfPCell(new Paragraph(" ",font));
		cell2.setBorderWidthTop(0);
		cell2.setBorderWidthLeft(0);
		cell2.setBorderWidthRight(0);
		cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.addCell(cell);
		table.addCell(cell2);
		
		cell = new PdfPCell(new Paragraph("电话：",font));
		cell.setBorderWidthTop(0);
		cell.setBorderWidthLeft(0);
		cell.setBorderWidthRight(0);
		cell.setMinimumHeight(40);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell2 = new PdfPCell(new Paragraph(" ",font));
		cell2.setBorderWidthTop(0);
		cell2.setBorderWidthLeft(0);
		cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.addCell(cell);
		table.addCell(cell2);
		
		doc.add(table);
	}
	
	private static void page3(Document doc,Font Sec_titleFont,Font font) throws DocumentException{
		doc.newPage();
		PdfPTable table = new PdfPTable(4);
		table.setWidthPercentage(90);
		table.getDefaultCell().setMinimumHeight(30);
		table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.setSplitLate(false);		
		int widths[] = {23,16,9,12};//设置每列宽度比例   
        table.setWidths(widths);  
		
        PdfPCell cell = new PdfPCell(new Paragraph("二、项目实施及资金使用情况\n\n",Sec_titleFont));
        PdfPCell cell2;
        cell.setColspan(4);
        cell.setBorder(0);
        cell.setPaddingTop(50);
        table.addCell(cell);
        
        table.addCell(new Paragraph("计划建设地点：",font));
		cell = new PdfPCell(new Paragraph(" ",font));
		cell.setColspan(3);
		table.addCell(cell);
		
		table.addCell(new Paragraph("实际建设地点：",font));
		cell = new PdfPCell(new Paragraph(" ",font));
		cell.setColspan(3);
		table.addCell(cell);
		
		table.addCell(new Paragraph("项目的建设内容：",font));
		cell = new PdfPCell(new Paragraph(" ",font));
		cell.setColspan(3);
		cell.setMinimumHeight(200);
		table.addCell(cell);
		
		table.addCell(new Paragraph("未完成的建设内容及原因：",font));
		cell = new PdfPCell(new Paragraph(" ",font));
		cell.setColspan(3);
		table.addCell(cell);
		
		table.addCell(new Paragraph("有无变更项目内容（变更原因）：",font));
		cell = new PdfPCell(new Paragraph(" ",font));
		cell.setColspan(3);
		table.addCell(cell);
		
		table.addCell(new Paragraph("财政补助资金（万元）：",font));
		cell = new PdfPCell(new Paragraph(" ",font));
		table.addCell(cell);
		
		table.addCell(new Paragraph("实际到位资金（万元）：",font));
		cell = new PdfPCell(new Paragraph(" ",font));
		table.addCell(cell);
		
		table.addCell(new Paragraph("资金规范支出率（％）：",font));
		cell = new PdfPCell(new Paragraph(" ",font));
		table.addCell(cell);
//		财务制度建立及会计行为规范情况：
		table.addCell(new Paragraph("财务制度建立及会计行为规范情况：",font));
		cell = new PdfPCell(new Paragraph(" ",font));
		cell.setColspan(3);
		table.addCell(cell);
//        资金专帐管理、专款专用情况：
		table.addCell(new Paragraph("资金专帐管理、专款专用情况：",font));
		cell = new PdfPCell(new Paragraph(" ",font));
		cell.setColspan(3);
		table.addCell(cell);
//		截留、挤占、挪用、侵占、套取建设资金情况：
		table.addCell(new Paragraph("截留、挤占、挪用、侵占、套取建设资金情况：",font));
		cell = new PdfPCell(new Paragraph(" ",font));
		cell.setColspan(3);
		table.addCell(cell);
		
//		预期效益（社会效益和经济效益）：
		cell = new PdfPCell(new Paragraph("预期效益（社会效益和经济效益）：",font));
		cell.setBorderWidthBottom(0);
		cell.setColspan(4);
		table.addCell(cell);
		
		cell = new PdfPCell(new Paragraph(" ",font));
		cell.setColspan(4);
		cell.setBorderWidthTop(0);
		cell.setMinimumHeight(150);
		table.addCell(cell);
		
		
        doc.add(table);
	}
	
	/**
	 * 第三頁
	 * 可以重复调用
	 * @param doc
	 * @param font
	 * @param Sec_titleFont
	 * @throws DocumentException
	 * 
	 */
	private static void page4(Document doc,Font font,Font Sec_titleFont,String title,String content,int top ,int bottom,int left,int right) throws DocumentException{
		doc.newPage();
		PdfPTable table = new PdfPTable(1);          
		table.setWidthPercentage(100);
		table.setSplitLate(false);
		String oneStr = content;
		PdfPCell cell =  new PdfPCell(new Paragraph(title,Sec_titleFont));
		cell.setMinimumHeight(30);
		//设置上下左右边框样式
		cell.setBorderWidthTop(top);
		cell.setBorderWidthBottom(bottom);
		cell.setBorderWidthLeft(left);
		cell.setBorderWidthRight(right);
		table.addCell(cell);
		
		cell = new PdfPCell(new Paragraph(oneStr,font));
        cell.setMinimumHeight(700);
        cell.setBorderWidth(1);
        cell.setBorderWidthTop(0);
        table.addCell(cell);
		
        doc.add(table);
	}
	
	private static void page5(Document doc,Font font,Font Sec_titleFont) throws DocumentException{
		doc.newPage();
		PdfPTable table = new PdfPTable(1);          
		table.setWidthPercentage(100);
		table.setSplitLate(false);
		String oneStr = "";
		PdfPCell cell =  new PdfPCell(new Paragraph("四、验收结论",Sec_titleFont));
		cell.setMinimumHeight(30);
		//设置上下左右边框样式
		cell.setBorderWidthTop(0);
		cell.setBorderWidthBottom(1);
		cell.setBorderWidthLeft(0);
		cell.setBorderWidthRight(0);
		table.addCell(cell);
		
		cell = new PdfPCell(new Paragraph(oneStr,font));
        cell.setMinimumHeight(500);
        cell.setBorderWidth(1);
        cell.setBorderWidthTop(0);
        cell.setBorderWidthBottom(0);
        table.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("验收组长(签名): \n\n日期: \n\n",font));
        cell.setMinimumHeight(50);
        cell.setBorderWidth(1);
        cell.setBorderWidthTop(0);
        table.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("验收组织单位意见：",font));
        cell.setMinimumHeight(30);
        cell.setBorderWidth(1);
        cell.setBorderWidthBottom(0);
        table.addCell(cell);
        //内容
        cell = new PdfPCell(new Paragraph(" ",font));
        cell.setMinimumHeight(100);
        cell.setBorderWidth(1);
        cell.setBorderWidthTop(0);
        cell.setBorderWidthBottom(0);
        table.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("盖章：                  日期：",font));
        cell.setMinimumHeight(30);
        cell.setBorderWidth(1);
        cell.setBorderWidthTop(0);
        table.addCell(cell);
        
        doc.add(table);
	}
	
	private static void page6(Document doc,Font Sec_titleFont,Font font) throws DocumentException{
		doc.newPage();
		PdfPTable table = new PdfPTable(5);          
		table.setWidthPercentage(90);
		table.setSplitLate(false);
		PdfPCell cell =  new PdfPCell(new Paragraph("五、参加验收人员名单及签名",Sec_titleFont));
		cell.setMinimumHeight(30);
		//设置上下左右边框样式
		cell.setBorderWidthTop(0);
		cell.setBorderWidthBottom(1);
		cell.setBorderWidthLeft(0);
		cell.setBorderWidthRight(0);
		cell.setColspan(5);
		table.addCell(cell);
               
		table.addCell(new PdfPCell(new Paragraph("姓名",font)));
		table.addCell(new PdfPCell(new Paragraph("单位",font)));
		table.addCell(new PdfPCell(new Paragraph("职务",font)));
		table.addCell(new PdfPCell(new Paragraph("电话",font)));
		table.addCell(new PdfPCell(new Paragraph("签名",font)));
		
		for (int i = 0; i < 10; i++) {
			table.addCell(new PdfPCell(new Paragraph(" ",font)));
			table.addCell(new PdfPCell(new Paragraph(" ",font)));
			table.addCell(new PdfPCell(new Paragraph(" ",font)));
			table.addCell(new PdfPCell(new Paragraph(" ",font)));
			table.addCell(new PdfPCell(new Paragraph(" ",font)));
		}
		int i = 0;
		for (PdfPRow row : (ArrayList<PdfPRow>) table.getRows()) {
			if(i==0){
				i++;
				continue;
			}
			for (PdfPCell cells : row.getCells()) {
				
				if (cells != null) {
					cells.setPadding(8.0f);
					cells.setBorderWidth(1);
					cells.setHorizontalAlignment(Element.ALIGN_CENTER);
					cells.setVerticalAlignment(Element.ALIGN_MIDDLE);
				}
			}
		}
		doc.add(table);
	}
	
	/**
	 * 主方法
	 * @throws DocumentException
	 * @throws IOException
	 */
	public static void createPDF() throws DocumentException, IOException{
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Document doc = new Document(PageSize.A4);
		PdfWriter pw = PdfWriter.getInstance(doc, new FileOutputStream("d:\\惠州市水产良种推进工程项目验收表.pdf"));
		// 文档标题
		doc.addTitle("惠州市水产良种推进工程项目验收表");
		/*System.out.println(content.getItemValueAsString("项目名称"));*/
		// 文档主题
		doc.addSubject("惠州市水产良种推进工程项目验收表");
		// 文档作者
		doc.addAuthor("zmz");
		// 文档创建人
		doc.addCreator("zmz");
		// 关键字
		doc.addKeywords("惠州市水产良种推进工程项目验收表");
		doc.open();
		//标题字体设置
		BaseFont bfTitle = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
				BaseFont.NOT_EMBEDDED);
		Font titleFont = new Font(bfTitle, 26, Font.BOLD);
		//内容字体
		BaseFont bfComic = BaseFont.createFont("C:\\Windows\\Fonts\\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
		Font font = new Font(bfComic, 14, Font.NORMAL);
		Paragraph bank_par = new Paragraph("\n\n\n\n", font);
		//小标题
		Font Sec_titleFont = new Font(bfTitle, 18, Font.BOLD);
		//第一页
		page1(doc, bank_par, titleFont, font, date, sdf);
//		一、项目基本情况表
		page2(doc,Sec_titleFont,font);
//		二、项目实施及资金使用情况
		page3(doc,Sec_titleFont,font);
//      实际效益（社会效益和经济效益）：
		int top = 1,bottom = 0,left = 1,right = 1;
        page4(doc,font,Sec_titleFont,"实际效益（社会效益和经济效益）：","222222",top,bottom,left,right);
//      三、项目实施情况总结
        page4(doc,font,Sec_titleFont,"三、项目实施情况总结","包括项目执行情况、资金使用情况、效益分析、存在问题及建议（可附页）\n ",0,1,0,0);
//      四、验收结论
        page5(doc,font,Sec_titleFont);
//      五、参加验收人员名单及签名
        page6(doc,Sec_titleFont,font);
		
		doc.close();
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			createPDF();
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
}
